package com.li.controller;

import com.li.pojo.Weight;
import com.li.service.WeightService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.nio.file.WatchService;
import java.util.Collection;

@Controller
public class WeightController {
    @Resource
    WeightService service;

    @RequestMapping("/weight")
    public String list(Model model){
        Collection<Weight> weights=service.allWeight();
        model.addAttribute("weights",weights);
        return "weight/list";
    }
    @GetMapping(value = "/toweight")
    public String toweight(Model model){

        return "weight/add";
    }
    @RequestMapping(value = "/weight/add")
    //@ResponseBody
    public String addWeight(Weight weight){
        if(service.addWeight(weight))return "redirect:/weight";
        return "课程名重复，添加失败";
    }
    @RequestMapping(value = "/weight/query")
    public String queryWeight(){
        return "";
    }

    @GetMapping("/weight/{icourse}")
    public String toupdateWeight(
            @PathVariable("icourse")String icourse, Model model){
        Weight weight=new Weight(icourse);

        weight=service.queryWeight(weight);
        model.addAttribute("wei",weight);
        return "weight/update";
    }
    @RequestMapping(value = "/weight/update")
    public String updateWeight(Weight weight){
        service.updateWeight(weight);
        return "redirect:/weight";
    }
    @RequestMapping(value = "/deleteweight/{icourse}")
    public String deleteCourse2(@PathVariable("icourse")String icourse){
        Weight weight=new Weight(icourse);
        service.deleteWeight(weight);
        return "redirect:/weight";
    }
}
